package jsonlistview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.example.administrator.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import app.Application;
import httptool.getHttpUrl;
import httptool.takeUrlResource;


/**
 * Created by cch on 2016/6/7.
 */
public class JsonActivity1 extends AppCompatActivity {
    private MyAdaptert adapters;
    private ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list1);
        list = (ListView) findViewById(R.id.listview);
        getJson1();
    }

    public ArrayList<Map<String,String>> data = new ArrayList<Map<String,String>>();
    private void getJson1() {
        takeUrlResource.getJson(getApplicationContext(), getHttpUrl.index, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject jso = new JSONObject(s);
                    String retcode = jso.getString("retcode");
                    if (retcode.equals("200")){
                        JSONArray jna = jso.getJSONArray("news");
                        for (int i = 0;i < jna.length();i++){
                            String title = jna.getJSONObject(i).getString("title");
                            String writer = jna.getJSONObject(i).getString("writer");
                            Map<String,String> map = new HashMap<String, String>();
                            map.put("title",title);
                            map.put("writer",writer);
                            Log.e("这个是文章的标题跟内容", title + ">>>>>" + writer);
                            JSONArray img = jna.getJSONObject(i).getJSONArray("imgs");
                            for (int j = 0;j < img.length();j++){
                                String imge = img.getString(j);
                                map.put("imgs",imge);
                                Log.e("这个是图片",imge);
                              }
                                data.add(map);
                            }
                        Log.e("这个是数据",data.size()+">>>>>>>>");
                        adapters = new MyAdaptert(getApplicationContext(),data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private class MyAdaptert extends BaseAdapter{
        private Context context;
        private ArrayList<Map<String,String>> data;
        public MyAdaptert(Context context, ArrayList<Map<String, String>> data) {
            this.context = context;
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.item1,parent,false);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.image);
                holder.title = (TextView) convertView.findViewById(R.id.tv1);
                holder.description = (TextView) convertView.findViewById(R.id.tv2);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            String title = data.get(position).get("title");
            String description = data.get(position).get("writer");
            String iv = data.get(position).get("imgs");
            Application.universalImageLoader.displayImage(iv,holder.iv,Application.options);
            holder.title.setText(title);
            holder.description.setText(description);
            return convertView;
        }
        class ViewHolder{
            private ImageView iv;
            private TextView title,description;
        }
    }
}
